package edu.pku.sei.mdabase.infrastructure.runtime;

import java.util.HashMap;
import java.util.List;

import edu.pku.sei.mdabase.core.IDataType;
import edu.pku.sei.mdabase.core.IRuleInvoker;
import edu.pku.sei.mdabase.infrastructure.metamodel.*;
import edu.pku.sei.queryscript.parser.Ast.Function;

public class Environment implements IRuleInvoker {
	
	protected HashMap<String,Object> typeMap = new HashMap<String,Object>();

	public HashMap<String, Object> getTypeMap() {
		return typeMap;
	}

	public void setTypeMap(HashMap<String, Object> typeMap) {
		this.typeMap = typeMap;
	}
	
	private HashMap<String, Function> functions = new HashMap<String, Function>();
	
	public HashMap<String, Function> getFunctions()
	{
		return functions;
	}
	
	public void setFunctions(HashMap<String, Function> functions)
	{
		this.functions = functions;
	}
	
	public Function getFunction(String name)
	{
		return functions.get(name);
	}
	
	public boolean registFunction(String name, Function func)
	{
		if (functions.containsKey(name))
			return false;
		functions.put(name, func);
		return true;
	}
	
	
	public Object[] getTypeByPrefixName(String name){
		int sp = name.indexOf("::");
		
		if(sp==-1)
			return null;
		
		String prefix = name.substring(0, sp);
		String typeName = name.substring(sp+2);
		
		Object type = typeMap.get(prefix);
		
		if(type==null) return null;
		
		if(type instanceof MetaModelGraph) {
			MetaModelGraph metamodel = (MetaModelGraph)type;
		
		return new Object[] {prefix, metamodel.getMetaClass(typeName)};
		}
		else {
			System.out.println("unknown type "+name);
			return null;
		}
	}
	
	private HashMap<Object,Object> cacheSystem = new HashMap<Object,Object>();
	
	
	
	@SuppressWarnings("unchecked")
	public Object getCachedData(Object...args){
		if(args==null||args.length==0)
			return cacheSystem;
		
		HashMap<Object,Object> table = cacheSystem;
		
		for(int i=0;i<args.length-1;i++) {
			table = (HashMap<Object, Object>) table.get(args[i]);
			if(table==null)
				return null;
		}
				
		return table.get(args[args.length-1]);
	}
	
	@SuppressWarnings("unchecked")
	public void putCachedData(Object value,Object...args){
		if(args==null||args.length==0)
			return;
		
		HashMap<Object,Object> table = cacheSystem;
		
		for(int i=0;i<args.length-1;i++) {
			HashMap<Object,Object> ntable = (HashMap<Object, Object>) table.get(args[i]);
			if(ntable==null) {
				ntable = new HashMap<Object,Object>();
				table.put(args[i], ntable);
			}
			table = ntable;	
		}
				
		table.put(args[args.length-1],value);
	}

	@Override
	public List<String> getParamList(String ruleName) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object invokeRule(String name, Context base, String mode) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void initContext(Context base, Context toInit, String ruleName) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public Object invokeRule(String name, String mode)
	{
		// TODO Auto-generated method stub
		return null;
	}

}
